from flask import Blueprint, request, jsonify, json, make_response, redirect, g
from common.models.OauthCustomerBind import OauthCustomerBind
from common.models.Coupon import CustomerCoupon, ShopCoupon
from common.libs.customer.CustomerLibs import CustomerLibs
from common.libs.Helper import  get_current_date, get_current_time
from common.libs.UrlManager import UrlManager
from application import app,db
from web.controllers.BaseControllerHelper import *
from .Helper import *
from sqlalchemy import or_, and_
from decimal import *

route_api_coupon = Blueprint( 'coupon_page',__name__ )

@route_api_coupon.route("/get_coupon", methods=["GET"])
def get_coupon():
    resp = gene_response(msg="OK")
    data = request.args
    shop_id = get_values_by_request(data, "shop_id")
    is_used = get_values_by_request(data, "is_used")
    is_valid = get_values_by_request(data, "is_valid")
    c_id = g.customer_info.id
    data_list = []

    if not shop_id :
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)
        return jsonify(resp)

    coupon_id_list = CustomerCoupon.query\
        .filter(CustomerCoupon.c_id==c_id, CustomerCoupon.is_used==1, CustomerCoupon.is_valid==1,
                ShopCoupon.time_type!=1,ShopCoupon.shop_id==shop_id,
                ShopCoupon.end_time<get_current_date(),CustomerCoupon.sc_id==ShopCoupon.id)\
        .with_entities(CustomerCoupon.id).all()
    coupon_id_list = list(val.id for val in coupon_id_list)
    if coupon_id_list and len(coupon_id_list)!=0:
        db.session.query(CustomerCoupon).filter(CustomerCoupon.id.in_(coupon_id_list)).update({'is_valid': '2', "updated_time": get_current_time()}, synchronize_session=False)
        db.session.commit()

    if is_used and  is_valid :
        if int(is_used) == 1 and int(is_valid) == 1 :
            coupon_list = CustomerCoupon.query.filter(CustomerCoupon.c_id==c_id, ShopCoupon.shop_id==shop_id, CustomerCoupon.is_used==is_used, CustomerCoupon.is_valid==is_valid).all()
        else :
            coupon_list = CustomerCoupon.query.filter(
                and_(CustomerCoupon.c_id==c_id,ShopCoupon.shop_id==shop_id ,
                     or_(CustomerCoupon.is_used==2, CustomerCoupon.is_valid==2)
                )).all()
    else :
        coupon_list = CustomerCoupon.query.filter_by(shop_id=shop_id).all()

    if coupon_list and len(coupon_list) != 0:
        for coupon in coupon_list :
            temp_sc = create_scoupon_json(coupon.sc, ["name", "start_time", "end_time", "time_type", "full_cash", "minus_cash", "cash_type"])
            temp_c = create_ccoupon_json(coupon, ["id", "is_used", "is_valid"], temp_sc)
            data_list.append(temp_c)
        resp = gene_response(code=200, msg="OK", data=data_list)
    else :
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)

    return jsonify(resp)

@route_api_coupon.route("/get_used_coupon", methods=["GET"])
def get_used_coupon() :
    resp = gene_response(msg="OK")
    data = request.args
    shop_id = get_values_by_request(data, "shop_id")
    total_cash = get_values_by_request(data, "total_cash")
    c_id = g.customer_info.id
    data_list = []

    coupon_id_list = CustomerCoupon.query\
        .filter(CustomerCoupon.c_id == c_id, CustomerCoupon.is_used == 1, CustomerCoupon.is_valid == 1,
                ShopCoupon.time_type != 1, ShopCoupon.shop_id == shop_id,
                ShopCoupon.end_time < get_current_date(), CustomerCoupon.sc_id == ShopCoupon.id) \
        .with_entities(CustomerCoupon.id).all()
    coupon_id_list = list(val.id for val in coupon_id_list)
    if coupon_id_list and len(coupon_id_list) != 0:
        db.session.query(CustomerCoupon).filter(CustomerCoupon.id.in_(coupon_id_list)).update(
            {'is_valid': '2', "updated_time": get_current_time()}, synchronize_session=False)
        db.session.commit()


    coupon_list = CustomerCoupon.query.filter(CustomerCoupon.c_id == c_id,
                                              ShopCoupon.shop_id == shop_id,
                                              CustomerCoupon.is_used == 1,
                                              CustomerCoupon.is_valid == 1).all()
    if coupon_list :
        for coupon in coupon_list :
            if float(coupon.sc.full_cash) > float(total_cash) :
                continue
            if coupon.sc.time_type == 2 :
                now_date = get_current_date()
                end_date = date_format(coupon.sc.end_time, "%Y-%m-%d")
                if now_date != end_date :
                    continue
            temp_sc = create_scoupon_json(coupon.sc, ["name", "start_time", "end_time", "time_type", "full_cash", "minus_cash", "cash_type"])
            temp_c = create_ccoupon_json(coupon, ["id", "is_used", "is_valid"], temp_sc)
            data_list.append(temp_c)
        resp = gene_response(code=200, msg="OK", data=data_list)
    else :
        resp = gene_response(code=-1, msg="暂无信息", data=data_list)
    return jsonify(resp)